/* rcshwbs.p - Container Workbench Record Sort Routine                      */
/* Copyright 1986-2002 QAD Inc., Carpinteria, CA, USA.                      */
/* All rights reserved worldwide.  This is an unpublished work.             */
/*V8:ConvertMode=NoConvert                                                  */
/*K1Q4*/ /*V8:WebEnabled=No                                                 */
/*                  LAST MODIFIED: 06/05/96           BY: GWM *J0QY*        */
/*                  LAST MODIFIED: 06/05/96           BY: LAL *J13B*        */
/* REVISION: 8.6    LAST MODIFIED: 05/20/98 BY: *K1Q4* Alfred Tan           */
/* REVISION: 9.1    LAST MODIFIED: 08/12/00 BY: *N0KP* myb                  */


     {mfdeclre.i}


     /* INPUT VARIABLES */
     define input parameter abs_recid as recid no-undo.
     define input parameter tmp_prefix as character no-undo.
     define output parameter return_code as integer no-undo.

     /* LOCAL VARIABLES */
     define variable ship_from like abs_shipfrom no-undo.
     define variable cont_level as integer no-undo.
     define variable next_nbr as integer no-undo.

     /* BUFFERS */
     define buffer abs_temp for abs_mstr.


     /*****************************/
     /* POSSIBLE RETURN CODES     */
     /* 0 = NORMAL                */
     /* 1 = MAX LEVELS EXCEEDED   */
     /* 2 = RECORD ALREADY LOCKED */
     /* 3 = MAX RECORDS EXCEEDED  */
     /* 4 = NO RECORDS TO SORT    */
     /*****************************/

     MAIN:
     do:

        return_code = 0.

        find abs_mstr where recid(abs_mstr) = abs_recid
        exclusive-lock no-wait no-error.

        if locked(abs_mstr) then do:
           {mfmsg.i 7422 4} /* RECORD LOCKED - PLEASE TRY AGAIN LATER */
           return_code = 2.
           leave MAIN.
        end.

        /* NO RECORDS TO SORT */
        if not available abs_mstr then do:
           return_code = 4.
           leave MAIN.
        end.

        ship_from = abs_mstr.abs_shipfrom.


        /* FIND THE HIGHEST LEVEL RECORD */
        PARENT_LOOP:
        repeat:

           /* IF A NON TEMP NUMBER THEN SAVE */
           if index(abs_mstr.abs_shipto,"::") = 0 then
           abs_mstr.abs__qad05 = abs_mstr.abs_shipto.

           abs_mstr.abs_shipto = tmp_prefix.

           find abs_temp where abs_temp.abs_shipfrom = abs_mstr.abs_shipfrom
/*J13B*/       and abs_temp.abs_id <> ""
           and abs_temp.abs_id = abs_mstr.abs_par_id no-lock no-error.

           /* NO MORE PARENT RECORDS */
           if not available abs_temp then
           leave PARENT_LOOP.

           /* FIND AND LOCK THE PARENT RECORD */
           else
           find abs_mstr where recid(abs_mstr) = recid(abs_temp)
           exclusive-lock no-wait no-error.

           if locked(abs_mstr) then do:
          {mfmsg.i 7422 4} /* RECORD LOCKED - PLEASE TRY AGAIN LATER */
          return_code = 2.
          leave MAIN.
           end.

        end. /* PARENT_LOOP */

        /* NOW HAVE THE HIGHEST LEVEL ABS_MSTR IN THIS CHAIN */


        cont_level = 0.

        /* SET THE SORT ORDER AND LEVEL FOR THIS AND ALL CHILD RECORDS */
        {gprun.i ""rcctwba1.p""
             "(input recid(abs_mstr),
               input cont_level,
               input tmp_prefix,
               input-output next_nbr)"}

        if next_nbr = -1 then do:
           {mfmsg.i 758 4} /* MAXIMUM NUMBER OF LEVELS EXCEEDED */
           return_code = 1.
           leave MAIN.
        end.

        if next_nbr = -2 then do:
           {mfmsg.i 7422 4} /* RECORD LOCKED - TRY AGAIN LATER */
           return_code = 2.
           leave MAIN.
        end.

        next_nbr = next_nbr + 1.

        if next_nbr > 9999 then do:
           {mfmsg.i 759 4} /* MAXIMUM NUMBER OF RECORDS EXCEEDED */
           return_code = 3.
           leave MAIN.
        end.

     end. /* MAIN */
